﻿using Apache.IoTDB;
using Apache.IoTDB.DataStructure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharp_Example
{
    public partial class Form1 : Form
    {
        SessionPool sessionPool;

        public Form1()
        {
            InitializeComponent();
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            // listView初始化
            this.listView1.Columns.Clear();
            this.listView1.Columns.Add("时间", 130);
            this.listView1.Columns.Add("状态", 60);

            // 初始化session
            sessionPool = new SessionPool("47.122.63.169", 6667, 2);

            // 开启session
            await sessionPool.Open(false);
        }

        private async void insertBtn_Click(object sender, EventArgs e)
        {
            var measures = new List<string> { "status" };
            var values = new List<object> { 123 };
            var rowRecord = new RowRecord(DateTime.Now, values, measures);
            var result = await sessionPool.InsertRecordAsync("root.test.test", rowRecord);
            Console.WriteLine("insert result="+result);
        }

        private async void queryBtn_Click(object sender, EventArgs e)
        {
            var result = await sessionPool.ExecuteQueryStatementAsync("select status from root.test.test order by time desc");
            Console.WriteLine("query done, rowCount="+result.RowCount);
            result.ShowTableNames();

            //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度 
            this.listView1.BeginUpdate();   
            this.listView1.Items.Clear();
            List<ListViewItem> items = new List<ListViewItem>();

            for (int i=0; i< result.RowCount; i++)
            {
                RowRecord rowRecord = result.Next();

                string[] rows = new string[2];
                rows[0] = DateTimeOffset.FromUnixTimeMilliseconds(rowRecord.Timestamps).LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                rows[1] = rowRecord.Values[0].ToString();
                items.Add(new ListViewItem(rows));

                //string values = DateTimeOffset.FromUnixTimeMilliseconds(rowRecord.Timestamps).ToString("yyyy-MM-dd HH:mm:ss")+"\t\t";
                //for(int j = 0;j < rowRecord.Values.Count; j++)
                //{
                //    values += (rowRecord.Values[j].ToString()+"\t\t");
                //}
                //Console.WriteLine(values);
            }
            //while (result.HasNext())
            //{
            //    Console.WriteLine(result.Next());
            //}

            this.listView1.Items.AddRange(items.ToArray());
            //结束数据处理，UI界面一次性绘制。
            this.listView1.EndUpdate();  
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 关闭连接
            sessionPool.Close();
        }
    }
}
